package com.jx.yygh.email.service.impl;

import com.jx.yygh.email.service.EmailService;
import com.jx.yygh.vo.msm.MsmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    JavaMailSender javaMailSender;

    //原代码
    @Override
    @Async
    public void sendEmail(String email, String code) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("尚医通项目登录验证码");
        simpleMailMessage.setText("尊敬的:"+email+"您的注册校验验证码为：" + code + "有效期5分钟");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setFrom("1245926884@qq.com");
        javaMailSender.send(simpleMailMessage);
    }
//
//    @Override
//    public void send(MsmVo msmVo) {
//        if(!StringUtils.isEmpty(msmVo.getPhone())) {
//            String code = (String)msmVo.getParam().get("code");
//            this.sendEmail(msmVo.getPhone(),code);
//        }
//    }


    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            String code = (String)msmVo.getParam().get("code");
            this.sendEmail(msmVo.getPhone(),msmVo.getParam());
        }
        return true;
    }


    private void sendEmail(String email, Map<String,Object> param) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("尚医通项目登录验证码");
//        simpleMailMessage.setText("尊敬的:"+email+"您的注册校验验证码为：" + code + "有效期5分钟");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setFrom("1245926884@qq.com");
        param.put("simpleMailMessage",simpleMailMessage);
        javaMailSender.send(simpleMailMessage);
    }



}
